//
//  STWebController.m
//  car
//
//  Created by dd on 13-8-30.
//  Copyright (c) 2013年 ddxiaogui. All rights reserved.
//

#import "DXWebController.h"
#import "DXUtil.h"

@interface DXWebController ()

@end

@implementation DXWebController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initWithTitle:(NSString *)title url:(NSString *)url
{
    self = [super init];
    if (self)
    {
        _ttitle = title;
        _url = url;
    }
    
    return self;
}

- (void)loadView
{
    [super loadView];
    
    self.title = _ttitle;
    
    _loadingIndicatorView = [[UIActivityIndicatorView alloc] init];
    _loadingIndicatorView.frame = CGRectMake(280, 0, 40, 40);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_loadingIndicatorView];
    
    _webView = [[UIWebView alloc] init];
    _webView.frame = CGRectMake(0, 0, [DX screenWidth], [DX screenHeight] - 20 - 44);
    _webView.delegate = self;
    _webView.scalesPageToFit = YES;
    _webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_webView];
    
    AVAudioSession * audioSession = [AVAudioSession sharedInstance];
    NSError *error = nil;
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self loadUrl:_url];
}

- (void)loadUrl:(NSString *)url
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    request.cachePolicy = NSURLRequestReloadIgnoringCacheData;
    
    [_webView loadRequest:request];
}

- (void)loadHTML:(NSString *)html
{
    [_webView loadHTMLString:html baseURL:nil];
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [_loadingIndicatorView startAnimating];
    [SVProgressHUD showWithStatus:@"加载中..."];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_loadingIndicatorView stopAnimating];
    [SVProgressHUD dismiss];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [_loadingIndicatorView stopAnimating];
    [SVProgressHUD showErrorWithStatus:@"加载失败"];
}

@end
